package mapper;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import server.HttpServlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapperContext extends MapperBase {
    private String contextFilePath;
    private Map<String, HttpServlet> wrapperMap=new HashMap<>();

    public MapperContext(String name,String contextFilePath) {
        this.name = name;
        this.contextFilePath = contextFilePath;
        initWapper();
    }

    public String getContextFilePath() {
        return contextFilePath;
    }

    public void setContextFilePath(String contextFilePath) {
        this.contextFilePath = contextFilePath;
    }

    public Map<String, HttpServlet> getWrapperMap() {
        return wrapperMap;
    }

    public void setWrapperMap(Map<String, HttpServlet> wrapperMap) {
        this.wrapperMap = wrapperMap;
    }

    private void initWapper() {
        //解析web.xml
        InputStream inputStream = null;
        SAXReader saxReader = new SAXReader();
        try {
            inputStream = new FileInputStream(contextFilePath + "/web.xml");
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Element> list = rootElement.selectNodes("//servlet");
            for (int i = 0; i < list.size(); i++) {
                Element element = list.get(i);
                Element servletnameElement = (Element) element.selectSingleNode("servlet-name");
                String servletname = servletnameElement.getStringValue();
                Element servletclassElement = (Element) element.selectSingleNode("servlet-class");
                String servletclass = servletclassElement.getStringValue();

                //根据servlet-name的值找url-pattern
                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletname + "']");
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
                URLClassLoader loader =
                        new URLClassLoader(new URL[]{new File(contextFilePath).toURI().toURL()});
                Class myClass = loader.loadClass(servletclass);
                wrapperMap.put(urlPattern, (HttpServlet) myClass.newInstance());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


}
